package com.example.beautymakeup.service.impl;

import com.example.beautymakeup.dao.CommentDao;
import com.example.beautymakeup.dao.ShopDao;
import com.example.beautymakeup.domain.PageBean;
import com.example.beautymakeup.pojo.Comment;
import com.example.beautymakeup.pojo.Hot;
import com.example.beautymakeup.pojo.Shop;
import com.example.beautymakeup.service.ShopService;
import com.example.beautymakeup.utils.JwtUtil;
import com.example.beautymakeup.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopServiceImpl  implements ShopService {
    @Autowired
    private ShopDao shopDao;

    @Autowired
    private CommentDao commentDao;

    @Autowired
    private TimeUtil timeUtil;

    @Override
    public Shop getByUid(String token, int uid) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if (!"admin".equals(subject)){
            Shop byUid = shopDao.getByUid(uid);
            List<Comment> byShopId = commentDao.getByShopId(uid);
            for (Comment comment:
                    byShopId) {
                comment.setDate(timeUtil.getTime(comment.getDate()));
            }
            byUid.setComments(byShopId);
            return byUid;
        }
        return null;
    }

    @Override
    public PageBean<Shop> get(String token, int page, int size) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if (!"admin".equals(subject)){
            List<Shop> hots = shopDao.get((page - 1) * size, size);
            int count = shopDao.getCount();
            PageBean<Shop> pageBean = new PageBean<>();
            pageBean.setTotalCount(count);
            pageBean.setRows(hots);
            return pageBean;
        }
        return null;
    }

    @Override
    public PageBean<Shop> getAll(String token, int page, int size) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if ("admin".equals(subject)){
            List<Shop> hots = shopDao.getAll((page - 1) * size, size);
            int count = shopDao.getAllCount();
            PageBean<Shop> pageBean = new PageBean<>();
            pageBean.setTotalCount(count);
            pageBean.setRows(hots);
            return pageBean;
        }
        return null;
    }

    @Override
    public PageBean<Shop> getByTypeId(String token, int classifyId) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if ("admin".equals(subject)){
            List<Shop> hots = shopDao.getByTypeId(classifyId);
            PageBean<Shop> pageBean = new PageBean<>();
            pageBean.setRows(hots);
            return pageBean;
        }
        return null;
    }

    @Override
    public boolean update(String token, Shop shop) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if("admin".equals(subject)){
            return shopDao.update(shop) == 1;
        }
        return false;
    }

    @Override
    public boolean updateType(String token, Shop shop) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if("admin".equals(subject)){
            return shopDao.updateType(shop) == 1;
        }
        return false;
    }

    @Override
    public boolean add(String token, Shop shop) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if("admin".equals(subject)){
            return shopDao.add(shop) == 1;
        }
        return false;
    }

    @Override
    public boolean ban(String token, Shop shop) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if("admin".equals(subject)){
            return shopDao.ban(shop) == 1;
        }
        return false;
    }

    @Override
    public boolean delete(String token, Shop shop) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if("admin".equals(subject)){
            return shopDao.delete(shop) == 1;
        }
        return false;
    }

    @Override
    public boolean hot(String token, Shop shop) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if("admin".equals(subject)){
            return shopDao.hot(shop) == 1;
        }
        return false;
    }

    @Override
    public PageBean<Shop> getHotShop(String token, int hotId, int page, int size) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if (!"admin".equals(subject)){
            List<Shop> hots = shopDao.getHotShop(hotId,(page - 1) * size, size);
            int count = shopDao.getHotCount(hotId);
            PageBean<Shop> pageBean = new PageBean<>();
            pageBean.setTotalCount(count);
            pageBean.setRows(hots);
            return pageBean;
        }
        return null;
    }

    @Override
    public PageBean<Shop> getTypeShop(String token, int classifyId, int page, int size) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if (!"admin".equals(subject)){
            List<Shop> hots = shopDao.getTypeShop(classifyId,(page - 1) * size, size);
            int count = shopDao.getTypeCount(classifyId);
            PageBean<Shop> pageBean = new PageBean<>();
            pageBean.setTotalCount(count);
            pageBean.setRows(hots);
            return pageBean;
        }
        return null;
    }

    @Override
    public PageBean<Shop> search(String token, String shopName, int page, int size) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if (!"admin".equals(subject)){
            List<Shop> hots = shopDao.search(shopName,(page - 1) * size, size);
            int count = shopDao.getSearchCount(shopName);
            PageBean<Shop> pageBean = new PageBean<>();
            pageBean.setTotalCount(count);
            pageBean.setRows(hots);
            return pageBean;
        }
        return null;
    }

    @Override
    public int getShopCount(String token, int uid) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if(!"admin".equals(subject)){
            return shopDao.getShopCount(uid);
        }
        return 0;
    }
}
